package enterprise.web.tookit.account;

import java.security.Principal;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author regisrocha
 * 
 * @since 28/08/2012
 * 
 * @description Session Bean implementation class CreateAccountEJB
 */
@Stateless(mappedName = "CreateAccount")
public class CreateAccountEJB implements CreateAccountEJBRemote {

	@Resource
	private EJBContext context;

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * Account DAO
	 */
	private AccountDao accountDao;

	/**
	 * LOG
	 */
	private static final Logger log = Logger.getLogger(CreateAccountEJB.class);

	/**
	 * Default constructor.
	 */
	public CreateAccountEJB() {
	}

	@Override
	public Account createAccout(Account account) throws AccountException {
		if (this.validateAccount(account)) {
			accountDao.persist(account);
		}

		return account;
	}

	private boolean validateAccount(Account pAccount) throws AccountException {
		if (pAccount == null) {
			throw new AccountException("Account can't null");
		} else if (!StringUtils.isNotBlank(pAccount.getFirstname())) {
			throw new AccountException("Firstname can't null");
		} else if (!StringUtils.isNotBlank(pAccount.getGender())) {
			throw new AccountException("Gender can't null");
		} else if (!StringUtils.isNotBlank(pAccount.getUsername())) {
			throw new AccountException("Username can't null");
		} else if (!StringUtils.isNotBlank(pAccount.getPassword())) {
			throw new AccountException("Password can't null");
		}

		return true;

	}

	@PostConstruct
	public void init() {
		log.info("Starting CreateAccountEJB");

		try {
			accountDao = new AccountDaoImpl(entityManager);
		} catch (Exception e) {
			log.error("Erro ao inicializar dao account", e);
		}
	}

	@PreDestroy
	public void destroy() {
		log.info("Destroing CreateAccountEJB");
	}

	@PrePassivate
	public void passivate() {
		try {
			Principal user = context.getCallerPrincipal();
			log.info("Passivate CreateAccountEJB, client: " + user.getName());
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@PostActivate
	public void activate() {
		try {
			Principal user = context.getCallerPrincipal();
			log.info("Passivate CreateAccountEJB, client: " + user.getName());
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@Override
	public void logout(Account account) {

	}

	@Override
	public Account login(Account account) throws AccountException {
		if (account == null) {
			throw new AccountException("Account can't null");
		} else if (!StringUtils.isNotBlank(account.getUsername())) {
			throw new AccountException("Username can't null");
		} else if (!StringUtils.isNotBlank(account.getPassword())) {
			throw new AccountException("Password can't null");
		}

		try {
			return accountDao.login(account);
		} catch (AccountException e) {
			log.error("", e);
			throw e;
		} catch (Exception e) {
			log.error("", e);
			throw new AccountException(e);
		}
	}

	@Override
	public Account updateAccount(Account account) throws AccountException {
		try {
			if (account == null) {
				throw new AccountException("account can't null");
			} else if (account.getId() == null) {
				throw new AccountException("ID can't null");
			} else if (this.usernameModified(account)) {
				throw new AccountException("Username can't modified");
			}

			return accountDao.updateAccount(account);
		} catch (Exception e) {
			throw new AccountException(e);
		}
	}

	private boolean usernameModified(Account account) {
		return accountDao.usernameModified(account);
	}
}